package com.hksj.cockpit.service;

import com.hksj.base.query.BaseSpecialityQuery;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.vo.DeviceStatusVO;
import com.hksj.base.vo.PropertyVO;
import com.hksj.cbm.service.DeviceInstanceService;
import com.hksj.cockpit.mapper.ComprehensiveCockpitMapper;
import com.hksj.cockpit.vo.AlarmRecordVO;
import com.hksj.cockpit.vo.DeviceOverviewVO;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @ClassName CockpitService
 * @Description TODO
 * @Author @陈章
 * @Date 2024/8/9 9:59
 * @Version 1.0
 */
@Service
public class ComprehensiveCockpitService {
    @Autowired
    private ComprehensiveCockpitMapper mapper;
    @Autowired
    private DefaultDeviceService defaultDeviceService;
    @Autowired
    private DeviceInstanceService deviceInstanceService;

    public List<String> getCommonCodes() {
        List<String> codes = new ArrayList<>();
        codes.add(SpecialityModelCodeEnum.computer_room_security.getCode());
        codes.add(SpecialityModelCodeEnum.air_conditioning.getCode());
        codes.add(SpecialityModelCodeEnum.medical_gas.getCode());
        codes.add(SpecialityModelCodeEnum.differential_pressure.getCode());
        codes.add(SpecialityModelCodeEnum.cleanliness.getCode());
        codes.add(SpecialityModelCodeEnum.air_quality.getCode());
        return codes;
    }

    public List<PropertyVO> selectDevice(BaseSpecialityQuery query) {
        List<PropertyVO> list = new ArrayList();
        //取环境监测第一台设备
        String id = mapper.getFirstDevice(query);
        if (!"".equals(id)) {
            list = mapper.query(id);
        }
        return list;
    }

    public List<Map<String, Integer>> selectDeviceAlarmCountDistribution(BaseSpecialityQuery query) {
        query.setCode(getCommonCodes());
        List<Map<String, Integer>> list = mapper.selectDeviceAlarmCountDistribution(query);
        return list;
    }

    public List<AlarmRecordVO> queryAlarmRecord(BaseSpecialityQuery query) {
        query.setCode(getCommonCodes());
        List<AlarmRecordVO> list = mapper.queryAlarmRecord(query);
        return list;
    }

    public Map<String, String> queryDeviceOverview(BaseSpecialityQuery query) {
        query.setCode(getCommonCodes());
        Map<String, String> map = new LinkedHashMap<String, String>();
        List<DeviceOverviewVO> list = mapper.queryDeviceOverview(query);
        //设备总数
        String count = String.valueOf(list.size());
        //设备在线数
        String online = String.valueOf(list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getState().equals("online")).count());
        //设备离线数
        String offline = String.valueOf(list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getState().equals("offline")).count());
        //设备未激活数
        String notActive = String.valueOf(list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getState().equals("notActive")).count());
        //在线率、先检查检查设备总数是否为零
        String deviceOnlineRate = "0%";
        if ("0".equals(count)) {
            // 设定一个默认值或者返回一个特定的消息
            deviceOnlineRate = "0%";
        } else {
            deviceOnlineRate = Integer.valueOf(online) * 100 / Integer.valueOf(count) + "%";
        }
        map.put("count", count);
        map.put("online", online);
        map.put("offline", offline);
        map.put("notActive", notActive);
        map.put("deviceOnlineRate", deviceOnlineRate);

        //机房安全
        String computerCode = SpecialityModelCodeEnum.computer_room_security.getCode();

        //机房安全设备在线数
        long computerOnlineCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(computerCode) && DeviceOverviewVO.getState().equals("online")).count();
        //机房安全设备总数
        long computerCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(computerCode)).count();

        //空调机组
        String airCode = SpecialityModelCodeEnum.air_conditioning.getCode();

        //空调机组设备在线数
        long airOnlineCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(airCode) && DeviceOverviewVO.getState().equals("online")).count();
        //空调机组设备总数
        long airCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(airCode)).count();

        //医气
        String medicalCode = SpecialityModelCodeEnum.medical_gas.getCode();

        //医气设备在线数
        long medicalOnlineCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(medicalCode) && DeviceOverviewVO.getState().equals("online")).count();
        //医气设备总数
        long medicalCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(medicalCode)).count();

        //压差
        String differentialCode = SpecialityModelCodeEnum.differential_pressure.getCode();

        //压差设备在线数
        long differentialOnlineCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(differentialCode) && DeviceOverviewVO.getState().equals("online")).count();
        //压差设备总数
        long differentialCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(differentialCode)).count();

        //洁净度
        String cleanlinessCode = SpecialityModelCodeEnum.cleanliness.getCode();

        //洁净度设备在线数
        long cleanlinessOnlineCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(cleanlinessCode) && DeviceOverviewVO.getState().equals("online")).count();
        //洁净度设备总数
        long cleanlinessCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(cleanlinessCode)).count();

        //空气质量
        String qualityCode = SpecialityModelCodeEnum.air_quality.getCode();

        //空气质量设备在线数
        long qualityOnlineCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(qualityCode) && DeviceOverviewVO.getState().equals("online")).count();
        //空气质量设备总数
        long qualityCount = list.stream().filter(DeviceOverviewVO -> DeviceOverviewVO.getCode() != null && DeviceOverviewVO.getCode().equals(qualityCode)).count();

        map.put("computerOnlineCount",String.valueOf(computerOnlineCount));
        map.put("computerCount",String.valueOf(computerCount));
        map.put("airOnlineCount", String.valueOf(airOnlineCount));
        map.put("airCount", String.valueOf(airCount));
        map.put("medicalOnlineCount", String.valueOf(medicalOnlineCount));
        map.put("medicalCount", String.valueOf(medicalCount));
        map.put("differentialOnlineCount", String.valueOf(differentialOnlineCount));
        map.put("differentialCount", String.valueOf(differentialCount));
        map.put("cleanlinessOnlineCount", String.valueOf(cleanlinessOnlineCount));
        map.put("cleanlinessCount", String.valueOf(cleanlinessCount));
        map.put("qualityOnlineCount", String.valueOf(qualityOnlineCount));
        map.put("qualityCount", String.valueOf(qualityCount));
        return map;
    }

    public List<DeviceStatusVO> queryDeviceList(BaseSpecialityQuery query) {
        //查询得到所有id
        List<String> deviceIdList = mapper.getDeviceIdList(query);
        List<DeviceStatusVO> list = new ArrayList<>();
        if (!ObjectUtils.isEmpty(deviceIdList)) {
            list = mapper.selectDeviceList(query, deviceIdList);
            defaultDeviceService.wrapAddr(list);
        }
        return list;
    }
}
